-- 公会成员战功榜
local RankNormalContentPanel = app.mvc.createPanelClass(...)
local ListViewVertical = require("app.widget.listview.ListViewVertical")

local RankContentPlayerCell = import("..renders.RankContentPlayerCell")
local RankContentPlayer2Cell = import("..renders.RankContentPlayer2Cell")
local RankContentGuildCell = import("..renders.RankContentGuildCell")

local RankTopFourColRender = import("..renders.RankTopFourColRender")
local RankTopThreeColRender = import("..renders.RankTopThreeColRender")

local RankDataConvertHelper = import("..RankDataConvertHelper")
--data =
function RankNormalContentPanel.createWithData(data, node)
    print("================>>>> ", data, node)
    local sprite = RankNormalContentPanel.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function RankNormalContentPanel.create(node)
    return RankNormalContentPanel.createWithData(nil, node)
end

function RankNormalContentPanel:init(data)
    self.super.init(self, data)
    local params = {
        cellConfigs = {
            [1] = {renderClass = RankContentPlayerCell, perNum = 1},
            [2] = {renderClass = RankContentPlayer2Cell, perNum = 1},
            [3] = {renderClass = RankContentGuildCell, perNum = 1},
        },
        cellTagMarkFunc = function(data)
            return data.tag
        end,
        cellEventProxy = function(eventName, params)
            executeFunc(self._onOperate, eventName, params)
        end,
        scrollEventListen = function()
            if self:isVisible() and self._data then
                local startIndex = self._listview:getHeadIndex()
                local page = math.floor( startIndex/ RankDataConvertHelper.PageCellNum) + self._data.startPage
                if self._curPage ~= page then
                    self._curPage = page
                    executeFunc(self._onOperate, "scrollToPage", page)
                end
            end
        end
    }
    self._listview = ListViewVertical.new(self._ui["Panel/ScrollView"], params)
    self._threeColTop = nil
    self._fourColTop = nil

    self._topRenders = {
        {tp = 1, render = nil},
        {tp = 2, render = nil},
    }
    self._cleanTag = nil
end

function RankNormalContentPanel:_createTopByType(tp)
    local render
    if tp == 1 then
        render = RankTopThreeColRender.create()
    elseif tp == 2 then
        render = RankTopFourColRender.create()
    end
    assert(render ~= nil, "_createTopByType render is nil ".. tp)
    self._ui["Panel/Node_Top"]:addChild(render)
    return render
end

function RankNormalContentPanel:refreshTop(topData)
    if not topData then
        return
    end
    local tp = topData.type
    for k, v in pairs(self._topRenders) do
        if v.tp == tp then
            if not v.render then
                v.render = self:_createTopByType(tp)
            else
                v.render:setVisible(true)
            end
            v.render:setData(topData)
        else
            if v.render then
                v.render:setVisible(false)
            end
        end
    end
end

function RankNormalContentPanel:jumpToPage(data, page, startIndex)
    self._data = data
    if not self._data then
        return
    end
    if not startIndex then
        local startPage = data.startPage or 1
        local realPage = page - startPage
        startIndex = realPage * RankDataConvertHelper.PageCellNum + 1
    end
    self._curPage = page
    self._listview:reloadData(data.contentData, {startIndex = startIndex})
    if not self._cleanTag  then
        self._ui["Panel/Panel_tips"]:setVisible(#data.contentData == 0)
        self:refreshTop(data.topData)
        self._cleanTag = true
    end
end

function RankNormalContentPanel:cleanTag()
    self._cleanTag = nil
end


return RankNormalContentPanel